package com.sh.service.impl;

import com.sh.bean.Employee;
import com.sh.util.FileUtil;
import com.sh.common.Constant;
import com.sh.service.IEmployee;

import java.util.*;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class IEmployeeImpl implements IEmployee {

    private Scanner sc = new Scanner(System.in);
    public List<Employee> employeeList = new ArrayList<>();

    @Override
    public void add() {
        // 是否存在该员工编号
        boolean flag = false;
        while (true) {
            System.out.println("请输入员工编号：");
            String id = sc.nextLine();
            if (isExist(flag, id)) {
                continue;
            }
            System.out.println("请输入姓名：");
            String name = sc.nextLine();
            System.out.println("请输入性别：");
            String gender = sc.nextLine();
            System.out.println("请输入出生年月：");
            String birth = sc.nextLine();
            System.out.println("请输入学历：");
            String edu = sc.nextLine();
            System.out.println("请输入职务：");
            String job = sc.nextLine();
            System.out.println("请输入工资：");
            String salary = sc.nextLine();
            System.out.println("请输入住址：");
            String address = sc.nextLine();
            System.out.println("请输入电话：");
            String phone = sc.nextLine();
            Employee employee = new Employee(id, name, gender, birth, edu, job, Double.parseDouble(salary), address, phone);
            employeeList.add(employee);
            if (  Constant.CONTINUE_TYPE_N.equalsIgnoreCase(getContinueType()  )){
                FileUtil.writeFile(employeeList);
                break;
            }
        }
    }



    private boolean isExist(boolean flag, String id) {
        for (Employee employee : employeeList) {
            if (id.equals(employee.getId())) {
                System.out.println("该员工编号已存在，请重新输入");
                flag = true;
                break;
            }
        }
        return flag;
    }

    @Override
    public void findAll() {
        if(employeeList.size() == 0){
            System.out.println("暂未查询到职工信息！！！");
        }else{
            FileUtil.readFile();
        }
    }

    @Override
    public void findByConditionOrSort() {
        while (true){
            System.out.println("请选择查询类型01或02:");
            String tytpe = sc.nextLine();
            if (Constant.QUERY_TYPE_ONE.equals(tytpe)){
                System.out.println("请输入学历信息:");
                printEduInfo();
            }else if (Constant.QUERY_TYPE_TWO.equals(tytpe)){
                sort();
            }else {
                System.out.println("抱歉，不存在该类型");
                continue;
            }

            if(Constant.CONTINUE_TYPE_N.equalsIgnoreCase(getContinueType())){
                break;
            }
        }
    }


    @Override
    public void edit() {
        while (true){
            System.out.println("请提供需要修改职工的原职工号:");
            String id = sc.nextLine();
            boolean flag = getEmployeeById(id);
            if (!flag){
                System.out.println("抱歉，没有找到该职工信息！！！");
                continue;
            }
            System.out.println("请输入修改后的职工姓名:");
            String name = sc.nextLine();
            updateEmployee(id,name);
            if(Constant.CONTINUE_TYPE_N.equalsIgnoreCase(getContinueType())){
                break;
            }
        }
    }

    @Override
    public void del() {
        while (true){
            System.out.println("请输入需要删除的职工号:");
            remove(sc.nextLine());
            if(Constant.CONTINUE_TYPE_N.equalsIgnoreCase(getContinueType())){
                break;
            }
        }
    }

    @Override
    public void sum() {
        System.out.println("职工总数为：" + employeeList.size() + "人");
    }


    private List<Employee> filterInfo(String edu) {
        List<Employee> emps = new ArrayList<>();
        Iterator<Employee> iterator = employeeList.iterator();
        while (iterator.hasNext()) {
            Employee next = iterator.next();
            String name = next.getEdu();
            if (name.equals(edu)) {
                emps.add(next);
            }
        }
        return emps;
    }

    private void remove(String empId) {
        boolean flag = false;
        Iterator<Employee> iterator = employeeList.iterator();
        while (iterator.hasNext()) {
            Employee next = iterator.next();
            if (empId.equals(next.getId())) {
                flag = true;
                iterator.remove();
            }
        }
        if (flag){
            System.out.println("删除"+empId+"号职工成功");
            FileUtil.writeFile(employeeList);
            return;
        }
        System.out.println("抱歉，没有查询到该职工！");


    }

    private boolean getEmployeeById(String id) {
        boolean flag = false;
        Iterator<Employee> iterator = employeeList.iterator();
        while (iterator.hasNext()) {
            Employee next = iterator.next();
            if (id.equals(next.getId())) {
                flag = true;
                System.out.println("职工信息：" + next.toString());
                break;
            }
        }
        return flag;
    }

    private void updateEmployee(String id,String name) {
        boolean flag = false;
        Iterator<Employee> iterator = employeeList.iterator();
        while (iterator.hasNext()) {
            Employee next = iterator.next();
            if (id.equals(next.getId())) {
                flag = true;
                next.setName(name);
                System.out.println("修改成功,修改之后的职工信息：" + next.toString());
                break;
            }
        }
        if (flag){
            FileUtil.writeFile(employeeList);
            return;
        }
    }

    private void sort() {
        System.out.println("-------------排序前-------------");
        for (Employee employee : employeeList) {
            System.out.println(employee.toString());
        }
        System.out.println("-------------排序后-------------");
        Collections.sort(employeeList);
        for (Employee employee : employeeList) {
            System.out.println(employee.toString());
        }

    }

    private void printEduInfo() {
        String edu = sc.nextLine();
        List<Employee> emps = filterInfo(edu);
        if(emps == null || emps.size() == 0){
            System.out.println("没有查询到学历信息！");
        }else{
            for (Employee emp : emps) {
                System.out.println(emp.toString());
            }
        }
    }

    private String getContinueType() {
        System.out.println("请问是否继续录入？Y/N");
        return sc.nextLine();
    }
}

